import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/service/umeng_service.dart';
import 'package:ball_lion/entity/my/my_focus_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/api/user_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/expert/items/expert_logo.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:ball_lion/widgets/focus_button.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyFocusUserView extends StatefulWidget {
  const MyFocusUserView({super.key});

  @override
  State<MyFocusUserView> createState() => _MyFocusUserViewState();
}

class _MyFocusUserViewState extends State<MyFocusUserView>
    with AutomaticKeepAliveClientMixin {
  int page = 1;
  int pageSize = 15;
  bool isLoading = true;
  List<MyFocusEntity> list = [];
  EasyRefreshController refresh =
      EasyRefreshController(controlFinishLoad: true);

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      await getNew();
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Column(
      children: [
        Expanded(
          child: Container(
            color: Colours.scaffoldBg,
            child: EasyRefresh(
              controller: refresh,
              onRefresh: () async {
                await getNew();
              },
              onLoad: () async {
                await getMore();
              },
              child: LoadingWidget<int>(
                isLoading: isLoading,
                data: list.length,
                noData: const NoDataWidget(tip: '暂无关注', needScroll: true),
                child: () => ListView.builder(
                    itemBuilder: (context, index) => _cell(index),
                    itemCount: list.length),
              ),
            ),
          ),
        ),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
          color: Colours.white,
          child: SafeArea(
            child: QsButton.large(
                text: '关注更多用户',
                textStyle: const TextStyle(
                    color: Colours.white,
                    fontSize: 16,
                    fontWeight: FontWeight.w500),
                height: 48,
                borderRadius: 8,
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                onPressed: () {
                  Get.toNamed(Routes.expertAll);
                }),
          ),
        )
      ],
    );
  }

  _cell(int index) {
    MyFocusEntity data = list[index];
    return Styles.card(
      margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
      child: GestureDetector(
        onTap: () {
          // Get.find<UmengService>().payOriginRoute = 'zjzy';
          Utils.onEvent('wd_wdgz_djzj');
          Get.toNamed(Routes.userPage,
              arguments: data.userId, parameters: {"type": "1"});
        },
        child: Container(
          color: Colours.white,
          child: Column(
            children: [
              Container(
                padding:
                    const EdgeInsets.symmetric(vertical: 15, horizontal: 16),
                child: Row(children: [
                  Container(
                    // alignment: Alignment.center,
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: Colours.greyEE,
                            width: 0.5,
                            strokeAlign: BorderSide.strokeAlignOutside),
                        borderRadius: BorderRadius.circular(21)),
                    child: ExpertLogo(
                      size: 42,
                      logo: data.userLogo!,
                      planCnt: data.planCount,
                    ),
                  ),
                  const SizedBox(width: 10),
                  QsText(
                    data.userName ?? '',
                    fontWeight: FontWeight.w500,
                  ),
                  const Spacer(),
                  FocusButton(
                    data.isFocus ?? 0,
                    userId: data.userId,
                    onTap: (p0) {
                      data.isFocus = p0;
                    },
                  ),
                ]),
              ),
              Container(
                margin: const EdgeInsets.only(left: 16),
                height: 0.5,
                color: Colours.greyEE,
              )
            ],
          ),
        ),
      ),
    );
  }

  Future getNew() async {
    page = 1;
    final data = await UserApi.myFocusUser(page, pageSize);
    if (data != null) {
      list = data;
      refresh.resetFooter();
    }
    isLoading = false;
    setState(() {});
  }

  Future getMore() async {
    page++;
    final data = await UserApi.myFocusUser(page, pageSize);
    if (data != null) {
      list.addAll(data);
      if (data.length < 15) {
        refresh.finishLoad(IndicatorResult.noMore);
      } else {
        refresh.finishLoad(IndicatorResult.success);
      }
    }
    setState(() {});
  }

  @override
  bool get wantKeepAlive => true;
}
